package com.ziyun.pop.modules.storeMaterialCheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.utility.New;
import com.ziyun.pop.common.utils.PurchaseUtils;
import com.ziyun.pop.modules.erpStockLog.entity.ErpStockLogEntity;
import com.ziyun.pop.modules.erpStockLog.service.ErpStockLogService;
import com.ziyun.pop.modules.storeMaterialCheck.entity.StoreMaterialCheckDetailEntity;
import com.ziyun.pop.modules.storeMaterialCheck.service.StoreMaterialCheckDetailService;
import com.ziyun.pop.modules.storeMaterialCheck.vo.StoreMaterialCheckDetailVo;
import com.ziyun.pop.modules.storeMaterials.entity.StoreStockMaterialEntity;
import com.ziyun.pop.modules.storeMaterials.service.StoreStockMaterialService;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.pop.modules.storeMaterialCheck.dao.StoreMaterialCheckDao;
import com.ziyun.pop.modules.storeMaterialCheck.entity.StoreMaterialCheckEntity;
import com.ziyun.pop.modules.storeMaterialCheck.service.StoreMaterialCheckService;
import org.springframework.transaction.annotation.Transactional;


@Service("storeMaterialCheckService")
public class StoreMaterialCheckServiceImpl extends ServiceImpl<StoreMaterialCheckDao, StoreMaterialCheckEntity> implements StoreMaterialCheckService {

    @Autowired
    private StoreMaterialCheckDetailService storeMaterialCheckDetailService;

    @Autowired
    private ErpStockLogService erpStockLogService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<StoreMaterialCheckEntity> page = (Page<StoreMaterialCheckEntity>) this.page(
                new Query<StoreMaterialCheckEntity>(params).getPage(),
                new QueryWrapper<StoreMaterialCheckEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<StoreMaterialCheckDetailVo> handlerToVo(Integer stockId) {
        List<StoreMaterialCheckDetailVo> resultList = this.baseMapper.getStoreMaterialCheckDetailVoListByStockId(stockId);
        return resultList;
    }

    /**
     * 盘点保存
     * @param storeMaterialCheckDetailVoList
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void checkSave(List<StoreMaterialCheckDetailVo> storeMaterialCheckDetailVoList,Integer stockId,SysUserEntity user) {
        StoreMaterialCheckEntity storeMaterialCheckEntity = new StoreMaterialCheckEntity();
        storeMaterialCheckEntity.setCreateName(user.getUsername());
        storeMaterialCheckEntity.setCreateTime(new Date());
        storeMaterialCheckEntity.setStockId(stockId);
        storeMaterialCheckEntity.setStatus(2);
        // 采购申请单号
        String str = "TS";
        String checkNo = PurchaseUtils.producePurchaseApplyNo(str,user.getSellerId().intValue());
        //判断采购申请单号是否重复
        boolean isRepeat = false;
        do {
            List<StoreMaterialCheckEntity> StoreMaterialCheckEntityList = baseMapper.selectList(
                    new QueryWrapper<StoreMaterialCheckEntity>().eq("check_no",checkNo)
            );
            if(StoreMaterialCheckEntityList != null && StoreMaterialCheckEntityList.size() > 0){
                isRepeat = true;
                checkNo = PurchaseUtils.producePurchaseApplyNo(str,user.getSellerId().intValue());
            }else{
                isRepeat = false;
            }
        }while (isRepeat);
        storeMaterialCheckEntity.setCheckNo(checkNo);
        this.baseMapper.insert(storeMaterialCheckEntity);
        List<StoreMaterialCheckDetailEntity> storeMaterialCheckDetailEntityList = New.arrayList();
        for (StoreMaterialCheckDetailVo storeMaterialCheckDetailVo : storeMaterialCheckDetailVoList) {
            StoreMaterialCheckDetailEntity storeMaterialCheckDetailEntity = new StoreMaterialCheckDetailEntity();
            storeMaterialCheckDetailEntity.setCheckId(storeMaterialCheckEntity.getCheckId());
            storeMaterialCheckDetailEntity.setCheckNo(storeMaterialCheckEntity.getCheckNo());
            storeMaterialCheckDetailEntity.setMaterialId(storeMaterialCheckDetailVo.getMaterialId());
            storeMaterialCheckDetailEntity.setActualQuantity(storeMaterialCheckDetailVo.getActualQuantity());
            storeMaterialCheckDetailEntity.setSystemQuantity(storeMaterialCheckDetailVo.getSystemQuantity());
            storeMaterialCheckDetailEntity.setUnit(storeMaterialCheckDetailVo.getUnit());
            storeMaterialCheckDetailEntity.setCreateName(user.getUsername());
            storeMaterialCheckDetailEntity.setCreateTime(new Date());
            storeMaterialCheckDetailEntityList.add(storeMaterialCheckDetailEntity);
        }
        storeMaterialCheckDetailService.saveBatch(storeMaterialCheckDetailEntityList);
    }

    @Override
    public R checkInfo(Integer checkId) {
        List<StoreMaterialCheckDetailVo> resultList = this.baseMapper.getCheckInfo(checkId);
        return R.ok().put("storeMaterialCheckDetailVoList",resultList);
    }

    @Override
    public void checkUpdate(List<StoreMaterialCheckDetailVo> storeMaterialCheckDetailVoList,String username) {
        List<StoreMaterialCheckDetailEntity> storeMaterialCheckDetailEntityList = New.arrayList();
        for (StoreMaterialCheckDetailVo storeMaterialCheckDetailVo : storeMaterialCheckDetailVoList) {
            StoreMaterialCheckDetailEntity storeMaterialCheckDetailEntity = new StoreMaterialCheckDetailEntity();
            storeMaterialCheckDetailEntity.setDetailId(storeMaterialCheckDetailVo.getDetailId());
            storeMaterialCheckDetailEntity.setCheckNo(storeMaterialCheckDetailVo.getCheckNo());
            storeMaterialCheckDetailEntity.setCheckId(storeMaterialCheckDetailVo.getCheckId());
            storeMaterialCheckDetailEntity.setActualQuantity(storeMaterialCheckDetailVo.getActualQuantity());
            storeMaterialCheckDetailEntityList.add(storeMaterialCheckDetailEntity);
        }
        if(storeMaterialCheckDetailEntityList != null && storeMaterialCheckDetailEntityList.size() > 0){
            ErpStockLogEntity erpStockLogEntity = new ErpStockLogEntity();
            erpStockLogEntity.setBillNo(storeMaterialCheckDetailEntityList.get(0).getCheckNo());
            erpStockLogEntity.setType(6);
            erpStockLogEntity.setCreateName(username);
            StringBuffer stringBuffer = new StringBuffer();
            List<StoreMaterialCheckDetailEntity> oldStoreMaterialCheckDetailEntityList = storeMaterialCheckDetailService.getCheckDetailList(storeMaterialCheckDetailEntityList.get(0).getCheckId());
            for(int i = 0;i<storeMaterialCheckDetailEntityList.size();i++){
                lableA:
                for(int j = 0;j<oldStoreMaterialCheckDetailEntityList.size();j++){
                    if(storeMaterialCheckDetailEntityList.get(i).getDetailId().intValue() == oldStoreMaterialCheckDetailEntityList.get(i).getDetailId().intValue()){
                        if(storeMaterialCheckDetailEntityList.get(i).getActualQuantity().compareTo(oldStoreMaterialCheckDetailEntityList.get(i).getActualQuantity()) != 0){
                            //做了修改,存入记录
                            stringBuffer.append("materialId:");
                            stringBuffer.append(oldStoreMaterialCheckDetailEntityList.get(j).getMaterialId());
                            stringBuffer.append("-修改实际数量:");
                            stringBuffer.append(oldStoreMaterialCheckDetailEntityList.get(j).getActualQuantity());
                            stringBuffer.append("--->");
                            stringBuffer.append(storeMaterialCheckDetailEntityList.get(i).getActualQuantity());
                            stringBuffer.append(";");
                        }
//                        oldStoreMaterialCheckDetailEntityList.remove(j);
//                        j--;
                        break lableA;
                    }
                }
            }
            erpStockLogEntity.setLogText(stringBuffer.toString());
            erpStockLogService.save(erpStockLogEntity);
            storeMaterialCheckDetailService.updateBatchById(storeMaterialCheckDetailEntityList);
        }
    }
}
